     USE [DB]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[StoredProcedureB]
       @Id bigint
    AS


    BEGIN

    SET NOCOUNT ON;
       
       SELECT  COUNT(DISTINCT o.Id)
          FROM
             Table1 n,
             Table2 o
          WHERE
             n.Id = @Id AND
             n.Id <> o.Id AND
             n.[Key] = o.[Key]
    END